﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_ImageSlideShow.Data
{
    public class DBImageSlideShow
    {
        readonly ImageSlideShowDc db = new ImageSlideShowDc();

        public ImageSlideShow GetOne(Guid itemGuid)
        {
            var result = from p in db.ImageSlideShows
                         where p.Guid.Equals(itemGuid)
                         select p;
            return result.FirstOrDefault();
        }
        
        public List<ImageSlideShow> GetAll(string[] exceptListModule)
        {
            var result = from p in db.ImageSlideShows
                         where !exceptListModule.Contains(p.ModuleID.ToString())
                         orderby p.SortOrder ascending, p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ImageSlideShow> GetAll(int moduleID)
        {
            var result = from p in db.ImageSlideShows
                         where p.ModuleID.Equals(moduleID)
                         orderby p.SortOrder ascending, p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ImageSlideShow> GetAllByCategoryID(int categoryId)
        {
            var result = from p in db.ImageSlideShows
                         where p.CategoryID.Equals(categoryId)
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public bool Delete(Guid itemGuid)
        {
            bool flag = false;
            ImageSlideShow item = GetOne(itemGuid);
            db.ImageSlideShows.DeleteOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool Create(int moduleID, int categoryId, string name, string url, string link, string linkTarget, bool isShow, int sortOrder)
        {
            bool flag = false;

            ImageSlideShow item = new ImageSlideShow
                                      {
                                          Guid = Guid.NewGuid(),
                                          ModuleID = moduleID,
                                          CategoryID = categoryId,
                                          Name = name,
                                          Url = url,
                                          Link = link,
                                          LinkTarget = linkTarget,
                                          IsShow = isShow,
                                          SortOrder = sortOrder,
                                          LastModified = DateTime.UtcNow
                                      };
            db.ImageSlideShows.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(Guid itemGuid, int categoryId, string name, string url, string link, string linkTarget, bool isShow, int sortOrder)
        {
            bool flag = false;

            ImageSlideShow item = GetOne(itemGuid);
            if (item != null)
            {
                item.CategoryID = categoryId;
                item.Name = name;
                item.Url = url;
                item.Link = link;
                item.LinkTarget = linkTarget;
                item.IsShow = isShow;
                item.SortOrder = sortOrder;
                item.LastModified = DateTime.UtcNow;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public void DeleteByModule(int moduleID)
        {
            var result = from p in db.ImageSlideShows
                         where p.ModuleID.Equals(moduleID)
                         select p;
            db.ImageSlideShows.DeleteAllOnSubmit(result);
            db.SubmitChanges();
        }
    }
}
